<template>
  <div>
    <div>
      <div class="header">
        <el-input v-model="name" placeholder="请输入行政区域名称"></el-input>
        <el-input v-model="code" placeholder="请输入行政区域编码"></el-input>
        <el-button type="primary" @click="search">
          <svg-icon icon-class="search" />
          查找
        </el-button>
        <el-button type="primary" @click="excelData">
          <svg-icon icon-class="form" />
          导出
        </el-button>
      </div>

      <div class="table">
        <el-table
          border
          :data="tableData"
          style="width: 100%"
          :default-sort="{ prop: 'id', order: 'descending' }"
        >
          <el-table-column prop="id" label="区域ID" sortable> </el-table-column>
          <el-table-column prop="pid" label="区域父ID"> </el-table-column>
          <el-table-column prop="name" label="区域名称"> </el-table-column>
          <el-table-column label="区域类型">
            <template slot-scope="scope">
              <span v-if="scope.row.type == 1"> 省 </span>
              <span v-else-if="scope.row.type == 2"> 市 </span>
              <span v-else> 区 </span>
            </template>
          </el-table-column>
          <el-table-column prop="code" label="区域编码"> </el-table-column>
        </el-table>
      </div>

      <div style="padding: 40px 0" class="footer">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="100"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { getregion } from "@/api/mall";
export default {
  props: [],
  data() {
    return {
      tableData: [],
      page: 1,
      total: 10,
      limit: 10,
      name: "", //搜索框行政区域名称
      code: "", //搜索框行政区域编码
    };
  },
  mounted() {
    this.getdata();
  },
  methods: {
    getdata() {
      let params = {
        page: this.page,
        limit: this.limit,
        name:this.name,
        code:this.code
      };
      getregion(params).then((res) => {
        console.log(res);
        if (res.errno == 0) {
          this.tableData = res.data.items;
          this.total = res.data.total;
        }
      });
    },
    // 改变每一页多少条数据
    handleSizeChange(e) {
      this.limit = e;
      this.getdata();
    },
    //改变当前页数
    handleCurrentChange(e) {
      this.page = e;
      this.getdata();
    },
    //搜索
    search() {
      this.getdata();
    },
    //导出excel表格
    excelData() {
      const headers = {
        区域ID: "id",
        区域父ID: "pid",
        区域名称: "name",
        区域类型: "type",
        区域编码:"code"
      };
      //懒加载
      import("@/vendor/Export2Excel").then((excel) => {
        const data = this.formatJson(headers, this.tableData);
        excel.export_json_to_excel({
          header: Object.keys(headers),
          data,
          filename: "行政区域",
        });
      });
    },
    // 该方法负责将数组转化成二维数组
    formatJson(headers, rows) {
      //首先遍历数组
      return rows.map((item) => {
        // item是对象  => 转化成只有值的数组 => 数组值的顺序依赖headers  {username: '张三'  }
        // Object.keys(headers)  => ["姓名", "手机号",...]
        return Object.keys(headers).map((key) => {
          if(item[headers[key]]==1){
            return "省"
          }
          if(item[headers[key]]==2){
            return "市"
          }
          if(item[headers[key]]==3){
            return "区"
          }
          return item[headers[key]]; // 得到 ['张三'，’129‘，’dd‘,'dd']
        });
      });
    },
  },
  computed: {},
};
</script>
<style lang="less" scoped>
.header {
  padding: 15px;
  display: flex;
  flex-wrap: wrap;
  /deep/.el-input {
    width: 200px;
    margin-left: 15px;
    .el-input__inner {
      width: 200px;
    }
  }
  .el-button {
    margin-left: 15px;
  }
}
.table {
  padding: 15px;
  /deep/.cell {
    text-align: center;
  }
}
</style>
